package com.bwf.jdbc3;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/6/2 10:23
 * @Copyright 博为峰
 */
public class StudentDaoImpl implements StudentDao {
    @Override
    public int insert(Student student, Connection conn) throws SQLException {
//        Connection conn = DBUtil.getConn();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement("insert into `t_student`(name,sex,salary,birthday,create_time,update_time,class_id) values (?, ?, ?, ?, ?, ?, ?)");
            preparedStatement.setString(1, student.getName());
            preparedStatement.setString(2, student.getSex());
            preparedStatement.setInt(3, student.getSalary());
            Date birthday = student.getBirthday();
            preparedStatement.setDate(4, new java.sql.Date(birthday.getTime()));
            Date createTime = student.getCreateTime();
            preparedStatement.setDate(5, new java.sql.Date(createTime.getTime()));
            preparedStatement.setDate(6, new java.sql.Date(student.getUpdateTime().getTime()));
            preparedStatement.setInt(7, student.getClassId());
            int i = preparedStatement.executeUpdate();
//            int n = 1 / 0;
            return i;
        } catch (SQLException e) {
            throw new RuntimeException("插入学生异常");
        } finally {
            preparedStatement.close();
        }

    }
}
